﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using VulnDBExplorer.Services;
using System.IO;
using Luna;


namespace VulnDBExplorer.Forms
{
	public partial class ConsoleForm : WeifenLuo.WinFormsUI.Docking.DockContent, IConsole
	{
		//private StringBuilder buffer;
		//private bool dirty = false;
		private List<ConsolePanel> consoles;

		public ConsoleForm()
		{
			InitializeComponent();
			HideOnClose = true;
			consoles = new List<ConsolePanel>();
			cboConsole.SelectedValueChanged += new EventHandler(cboConsole_SelectedIndexChanged);

			createConsole("", "Default");
			cboConsole.SelectedValue = consoles[0];
			consoles[0].Visible = true;
		}

		public IAppContext Context {
			get { return null; }
			set {
				if (value != null) value.addService(this);
			}
		}

		void cboConsole_SelectedIndexChanged(object sender, EventArgs e) {
			foreach (object obj in cboConsole.Items)
				(obj as ConsolePanel).Visible = (obj == cboConsole.SelectedValue);
		}

		public IConsole createConsole(string name, string text) {
			ConsolePanel console = consoles.FirstOrDefault(p => p.Name.Equals(name));
			if (console == null) {
				console = new ConsolePanel() { Name = name, Text = text, Visible = false, Dock = DockStyle.Fill};
				console.Parent = this;
				this.Controls.Add(console);
				consoles.Add(console);
				cboConsole.Items.Add(console);
			}
			cboConsole.Visible = consoles.Count > 1;
			return console;
		}

		#region IConsole Members

		void IConsole.write(string format, params object[] args)
		{
			(consoles[0] as IConsole).write(format, args);
		}

		void IConsole.writeLine(string format, params object[] args)
		{
			(consoles[0] as IConsole).writeLine(format, args);
		}

		void IConsole.log(string format, params object[] args)
		{
			(consoles[0] as IConsole).log(format, args);
		}

		#endregion
	}
}
